package additions;

import java.sql.*;
import java.util.Vector;

public class Database {

    public static void Register(String UserName, String Email, String Password) {
        Connection conn = null;

        try {
            Boolean UsernameTaken = false;

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:imdb");
            Statement stmt = conn.createStatement();
            String query = "";

            query = "SELECT UserName FROM Users";
            ResultSet results = stmt.executeQuery(query);

            while (results.next() && !UsernameTaken) {
                if (results.getString("UserName").compareToIgnoreCase(UserName) == 0) {
                    UsernameTaken = true;
                }
            }

            if (!UsernameTaken) {
                query = "INSERT INTO Users (UserName, EmailAddress, Password) VALUES ('" + UserName + "', '" + Email + "', '" + Password + "')";
                stmt.execute(query);
            } else {
                System.out.println("Username taken");
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        } finally {
            try {
                conn.close();
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
        }
    }

    public static int Login(String UserName, String Password) {
        Connection conn = null;
        int UserID = 0;

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:imdb");
            Statement stmt = conn.createStatement();
            ResultSet results = stmt.executeQuery("SELECT ContactID, UserName, Password FROM Users");
            Boolean found = false;

            while (results.next() && !found) {
                String DBUser = results.getString("UserName");
                String DBPass = results.getString("Password");
                if (DBUser.compareToIgnoreCase(UserName) == 0) {
                    found = true;
                    if (DBPass.compareTo(Password) == 0) {
                        UserID = results.getInt("ContactID");
                        //System.out.println("WOO");
                    } else {
                        UserID = -2;
                        //System.out.println("password incorrect");
                    }
                } else {
                    UserID = -1;
                    //System.out.println("username incorrect");
                }
            }

            stmt.close();
        } catch (Exception ex) {
            System.out.println("Cannot create SQL statement");
        } finally {
            try {
                conn.close();
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
        }
        return UserID;
    }

    public static int LookupUser(String SearchStr) {
        Connection conn = null;
        String query = "";

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:imdb");
            Statement stmt = conn.createStatement();

            query = "SELECT ContactID, UserName, EmailAddress FROM Users";
            ResultSet results = stmt.executeQuery(query);

            while (results.next()) {
                if (results.getString("UserName").compareToIgnoreCase(SearchStr) == 0) {
                    return results.getInt("ContactID");
                }

                if (results.getString("EmailAddress").compareToIgnoreCase(SearchStr) == 0) {
                    return results.getInt("ContactID");
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        } finally {
            try {
                conn.close();
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
        }

        return -1;
    }

    public static void AddContact(int UserID, int FriendID) {
        // will have to repeat, backwards for the friend's list.
        Connection conn = null;
        String query = "";

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:imdb");
            Statement stmt = conn.createStatement();

            query = "INSERT INTO Contacts (ContactID, FriendID) VALUES ('" + UserID + "', '" + FriendID + "')";
            stmt.execute(query);
            query = "INSERT INTO Contacts (ContactID, FriendID) VALUES ('" + FriendID + "', '" + UserID + "')";
            stmt.execute(query);
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        } finally {
            try {
                conn.close();
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
        }
    }

    public static void RemoveContact(int UserID, int FriendID) {
        // will have to repeat, backwards for the friend's list.
        Connection conn = null;
        String query = "";

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:imdb");
            Statement stmt = conn.createStatement();

            query = "DELETE FROM Contacts WHERE ContactID=" + UserID + " AND FriendID=" + FriendID;
            stmt.execute(query);

            query = "DELETE FROM Contacts WHERE ContactID=" + FriendID + " AND FriendID=" + UserID;
            stmt.execute(query);
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        } finally {
            try {
                conn.close();
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
        }
    }

    
    public static void GetContacts(int UserID, Vector Contacts) {
        // will have to repeat, backwards for the friend's list.
        Connection conn = null;
        String query = "";
  //      Contacts = new Vector();

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:imdb");
            Statement stmt = conn.createStatement();

            query = "SELECT FriendID FROM Contacts WHERE ContactID=" + UserID + "";
            ResultSet results = stmt.executeQuery(query);

            while (results.next()) {
                Contacts.addElement(results.getInt("FriendID"));
            }


        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        } finally {
            try {
                conn.close();
            } catch (Exception ex) {
                ex.printStackTrace(System.err);
            }
        }
    }
}

